package com.swmutil.swmutil.util;

import com.swmutil.swmutil.pojo.All_Param;
import com.swmutil.swmutil.sqlite.AllParamSql;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
public class PaneUtil {

    /**
     * 创建pane工具类
     */
    public static Pane newPane(All_Param param) {
        Integer id = param.getId();
        String signalDescription = param.getSignal_description();
        String value = String.valueOf(param.getValue());
        String unit = param.getUnit();

        Text text = new Text();
        while (signalDescription.length()<15){
            signalDescription="\u3000"+signalDescription;
        }
        text.setText(signalDescription);
        text.setLayoutX(10);

        TextField textField = new TextField();
        textField.setText(value);
        textField.setPrefWidth(100);
        textField.setLayoutX(20);
        textField.setLayoutY(10);
        textField.focusedProperty().addListener((observableValue, oldv, newv) -> {
            if(!newv){//失去焦点
                String text1 = textField.getText();
                AllParamSql.updateParam(Integer.valueOf(text1),id);
            }
        });

        Text textunit = new Text();
        textunit.setText(unit);
        textunit.setLayoutX(10);

        HBox hBox = new HBox(10); // 10像素间隔
        hBox.setLayoutX(param.getLayoutx()); // 可以设置Pane内部的布局位置
        hBox.setLayoutY(param.getLayouty());
        hBox.getChildren().addAll(text, textField,textunit);
        return hBox;
    }

    public static Pane newComboBox(All_Param param) {
        Integer id = param.getId();
        String signalDescription = param.getSignal_description();
        String options = param.getOptions();
        Integer value = param.getValue();

        String[] split = options.split(",");
        Text text = new Text();
        while (signalDescription.length()<15){
            signalDescription="\u3000"+signalDescription;
        }
        text.setText(signalDescription);
        ComboBox<String> cb = new ComboBox<>();
        cb.getItems().addAll(split);

        cb.setValue(split[value]);
        // 设置下拉选项改变时的事件处理
        cb.setOnAction(event -> {
            System.out.println(id);
            String selectedItem = cb.getValue();
            System.out.println("选中的项: " + selectedItem);
            int val=0;
            switch (selectedItem){
                case "正常":
                    val=0;
                    break;
                case "开路":
                    val=1;
                    break;
                case "对地短路":
                    val=2;
                    break;
                case "对电短路":
                    val=3;
                    break;
                default:
                    val=0;
                    break;
            }

            AllParamSql.updateParam(val,id);
        });

        String selectedItem = cb.getValue();
        System.out.println("选中的项: " + selectedItem);
        HBox hBox = new HBox(10); // 10像素间隔
        hBox.setLayoutX(param.getLayoutx()); // 可以设置Pane内部的布局位置
        hBox.setLayoutY(param.getLayouty());
        hBox.getChildren().addAll(text, cb);
        return hBox;
    }

    public static HBox newRadioButton(All_Param param){
        Integer id = param.getId();
        String signalDescription = param.getSignal_description();
        String options = param.getOptions();
        Text text = new Text();
        while (signalDescription.length()<15){
            signalDescription="\u3000"+signalDescription;
        }
        text.setText(signalDescription);
        text.setLayoutX(10);

        String[] split = options.split(",");
        // 创建ToggleGroup对象
        ToggleGroup toggleGroup = new ToggleGroup();

        // 创建第一个RadioButton
        RadioButton option1 = new RadioButton(split[0]);
        option1.setToggleGroup(toggleGroup);

        // 创建第二个RadioButton
        RadioButton option2 = new RadioButton(split[1]);
        option2.setToggleGroup(toggleGroup);
        switch (param.getValue()){
            case 1:
                option1.setSelected(true);
                break;
            case 0:
                option2.setSelected(true);
                break;
            default:
                break;
        }

        toggleGroup.selectedToggleProperty().addListener((observableValue, oldtog, newtog) -> {
            RadioButton selectedTog = (RadioButton) newtog;
            int value=0;
            if(selectedTog.equals(option2)){
                value=0;
            } else if (selectedTog.equals(option1)) {
                value=1;
            }
            System.out.println(selectedTog.getText());
            AllParamSql.updateParam(value,id);

        });

        HBox hBox = new HBox(10); // 10像素间隔
        hBox.setLayoutX(param.getLayoutx()); // 可以设置Pane内部的布局位置
        hBox.setLayoutY(param.getLayouty());
        hBox.getChildren().addAll(text,option1, option2);
        return hBox;
    }

}
